package tcln.sors.client;

import org.jdesktop.application.Application;
import org.springframework.context.ApplicationContext;

import tcln.sors.client.core.mvc.SingleApplicationController;
import tcln.sors.client.core.mvc.StandardControllerType;
import tcln.sors.client.core.utils.JFont;
import tcln.sors.client.main.MainScreenController;
import tcln.sors.client.main.MainScreenView;
import ch.elca.el4j.core.context.ModuleApplicationContext;
import ch.elca.el4j.core.context.ModuleApplicationContextConfiguration;

/**
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 * 
 */
public class SorsApplication extends Application {
    private static final JFont DEFAULT_FONT = new JFont().plain().size(14).name("Arial");
    private static final JFont DEFAULT_SMALL_FONT = new JFont().plain().size(12).name("Arial");

    private ApplicationContext beanApplicationContext;

    /**
     * The entry point of the application.
     * 
     * @param args
     */
    public static void main(String[] args) {
        launch(SorsApplication.class, args);
    }

    /**
     * Returns the single application instance for the SORS application.
     * 
     * @return
     */
    public static SorsApplication getAppInstance() {
        return (SorsApplication) getInstance();
    }

    /**
     * Returns the default font.
     * 
     * @return
     */
    public static JFont getDefaultFont() {
        return new JFont(DEFAULT_FONT);
    }

    /**
     * Returns the default small font.
     * 
     * @return
     */
    public static JFont getDefaultSmallFont() {
        return new JFont(DEFAULT_SMALL_FONT);
    }

    /**
     * Returns current application context for dependency injection.
     * 
     * @return the {@link #beanApplicationContext}.
     */
    public ApplicationContext getBeanContext() {
        return beanApplicationContext;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void initialize(String[] args) {
        super.initialize(args);

        ModuleApplicationContextConfiguration configuration = new ModuleApplicationContextConfiguration();
        configuration.setInclusiveConfigLocations(new String[] { "classpath:/core-config.xml",
                "classpath:/service-config.xml", "classpath:/dao-config.xml", "classpath:/datasource-config.xml",
                "classpath:/scenarios/dataaccess/hibernate/hibernateDefaultBeans.xml" });
        configuration.setAllowBeanDefinitionOverriding(true);
        beanApplicationContext = new ModuleApplicationContext(configuration);

        registerControllers();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void startup() {
        SingleApplicationController.getInstance().getFor(StandardControllerType.MAIN_PAGE, null).showView(true);
    }

    private void registerControllers() {
        SingleApplicationController.getInstance().register(StandardControllerType.MAIN_PAGE.getValue(),
                MainScreenController.class, MainScreenView.class, true);
    }
}
